// electron/main.cjs
const { app, BrowserWindow, ipcMain, dialog } = require('electron')
const path = require('path')

function createWindow() {
  const win = new BrowserWindow({
    width: 1000,
    height: 700,
    autoHideMenuBar: true,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
    },
    
  })

  if (process.env.VITE_DEV_SERVER_URL) {
    win.loadURL(process.env.VITE_DEV_SERVER_URL)
  } else {
    win.loadFile(path.join(__dirname, '../dist/index.html'))
  }
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit()
})

ipcMain.on('show-alert', (event, message) => {
  dialog.showMessageBox({
    type: 'info',
    title: '提示',
    message: message,
    buttons: ['确定']
  });
});

/*
Electron项目中不要使用alert函数 下面是替代
// 在 Vue 组件中
const { ipcRenderer } = require('electron');

// 替换原来的 alert('你好')
ipcRenderer.send('show-alert', '你好');
*/